package com.example.administrator.smartbutler.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.administrator.smartbutler.R;
import com.example.administrator.smartbutler.entity.MyUser;
import com.example.administrator.smartbutler.utils.RegexUtils;

import java.util.regex.Pattern;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * 项目名:SmartButler
 * 包名:com.example.administrator.smartbutler.ui
 * 文件名：RegisterActivity
 * 创建者：justin
 * Created by Administrator on 2018/1/19.
 * 描述：注册
 */

public class RegisterActivity extends BaseActivity implements View.OnClickListener {

    private EditText register_username;

    private EditText register_introduction;

    private EditText register_age;

    private RadioGroup register_sex;

    private EditText register_pwd;

    private EditText register_repwd;

    private EditText register_email;

    private Button register_btn_register;

    private boolean isSex;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);


        //初始化数据
        initView();
    }

    private void initView() {

        register_username = (EditText) findViewById(R.id.register_username);
        register_sex = (RadioGroup) findViewById(R.id.register_sex);
        register_introduction = (EditText) findViewById(R.id.register_introduction);
        register_age = (EditText) findViewById(R.id.register_age);
        register_pwd = (EditText) findViewById(R.id.register_pwd);
        register_repwd = (EditText) findViewById(R.id.register_repwd);
        register_email = (EditText) findViewById(R.id.register_email);
        register_btn_register = (Button) findViewById(R.id.register_btn_register);

        register_btn_register.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.register_btn_register:

                //得到文本并去空格
                String name = register_username.getText().toString().trim();
                String age = register_age.getText().toString().trim();
                String introduction = register_introduction.getText().toString().trim();
                String pwd = register_pwd.getText().toString().trim();
                String repwd = register_repwd.getText().toString().trim();
                String email = register_email.getText().toString().trim();


                //判断是否为空
                if (!TextUtils.isEmpty(name) & !TextUtils.isEmpty(age) & !TextUtils.isEmpty(repwd)
                        & !TextUtils.isEmpty(pwd) & !TextUtils.isEmpty(email)) {

                    if (RegexUtils.isUserName(name)) {


                        //判断两次密码是否一致
                        if (pwd.equals(repwd)) {

                            //判断性别
                            register_sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                                @Override
                                public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                                    if (checkedId == R.id.register_man) {
                                        isSex = true;
                                    } else if (checkedId == R.id.register_woman) {
                                        isSex = false;
                                    }
                                }
                            });


                            MyUser user = new MyUser();

                            user.setUsername(name);
                            user.setAge(Integer.parseInt(age));

                            //判断简介是否为空
                            if (!TextUtils.isEmpty(introduction)) {
                                user.setIntroduction(introduction);
                            } else {
                                user.setIntroduction("这个人很懒，什么也没留下！");
                            }

                            user.setPassword(pwd);
                            user.setSex(isSex);
                            user.setEmail(email);


                            //注册
                            user.signUp(new SaveListener<MyUser>() {

                                @Override
                                public void done(MyUser myUser, BmobException e) {

                                    if (e == null) {
                                        Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();

                                        //注册完成跳转到登录界面
                                        startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                                        finish();

                                    } else {
                                        Toast.makeText(RegisterActivity.this, "注册失败" + e.toString() + e.getErrorCode(), Toast.LENGTH_LONG).show();
                                    }
                                }
                            });


                        } else {
                            Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                        }

                    } else {
                        Toast.makeText(this, "用户名不能包含中文和特殊字符", Toast.LENGTH_SHORT).show();
                    }

                } else {
                    Toast.makeText(this, "输入框不能为空", Toast.LENGTH_SHORT).show();
                }

                break;
        }

    }
}
